<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>微博首页</h1>
    <div v-if="isLogin">
        <h1>亲爱的{{user.nickname}},欢迎回来!</h1>
        <a href="/insert.html">发微博</a>
        <a href="/myList.html">我的微博</a>
        <a href="javascript:void(0)" @click="logout()">退出登录</a>
    </div>
    <div v-else>
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <hr>
    <div v-for="weibo in arr">
        <a :href="'/detail.html?id='+weibo.id">
            <h3>{{weibo.nickname}}说:{{weibo.content}}</h3>
        </a>
    </div>
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            user: {},
            isLogin: false,
            arr: [],
        },
        methods: {
            logout() {
                if (confirm("Are you sure you want to logout?")) {
                    //不需要传参和增删改查,直接用get
                    axios.get("/logout").then(function (response) {
                        v.isLogin = false;
                    })
                }
            }
        },
        created: function () {
            //weibo/select发出请求,请求到的数据赋值给arr数组
            axios.get("/weibo/select").then(function (response) {
                v.arr = response.data;
            }),
                //发请求获取当前登录的用户对象(在服务器的会话对象里面)
                axios.get("/currentUser").then(function (response) {
                    //由于服务器端给客户端传递数据时会将数据转成Json字符串
                    //这样的话服务器响应的是null时,客户端接到的是""空字符串
                    if (response.data == "") {
                        v.isLogin = false;
                    } else {
                        v.isLogin = true;
                        v.user = response.data;
                    }
                })
        }
    })
</script>
</html>